package com.hsh.maoduo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsh.maoduo.common.Result;
import com.hsh.maoduo.mapper.SupplierMapper;
import com.hsh.maoduo.pojo.dto.SupplierDTO;
import com.hsh.maoduo.pojo.entity.Supplier;
import com.hsh.maoduo.pojo.vo.SupplierVO;
import com.hsh.maoduo.service.SupplierService;
import com.hsh.maoduo.util.DataUtil;
import com.hsh.maoduo.util.MyBeanUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author USER
 * @description 针对表【supplier】的数据库操作Service实现
 * @createDate 2023-12-23 16:35:07
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;

    @Override
    public List<SupplierDTO> getList(SupplierVO supplierVO) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
        wrapper.like(Strings.isNotBlank(supplierVO.getSupplierName()), "supplier_name", DataUtil.removeSpaceAndSymbols(supplierVO.getSupplierName()));
        return MyBeanUtils.copyList(supplierMapper.selectList(wrapper), SupplierDTO.class);
    }

    @Override
    public Result addOne(SupplierVO supplierVO) {
        int insertNum = supplierMapper.insert(MyBeanUtils.copy(supplierVO, Supplier.class));
        return Result.create(insertNum);
    }

    @Override
    public Result update(SupplierVO supplierVO) {
        int updateNum = supplierMapper.updateById(MyBeanUtils.copy(supplierVO, Supplier.class));
        return Result.create(updateNum);
    }

    @Override
    public Result delete(int id) {
        int deleteNum = supplierMapper.updateById(new Supplier().setId(id).setIsDelete(1));
        return Result.create(deleteNum);
    }
}




